package com.onluy.frameworkcloud.system.filter;
  
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.frameworkcloud.system.common.ParameterRequestWrapper;
import com.onluy.frameworkcloud.tools.DesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ParamDecodeFilter implements Filter {
	protected Logger log = LoggerFactory.getLogger(this.getClass());

	private  String secretkey= "e036hlqtculakx38tjw2u9gs";
	public String getSecretkey() {
		return secretkey;
	}
	public void setSecretkey(String secretkey) {
		this.secretkey = secretkey;
	}



	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest)request;
		String contenttype = ""+httpServletRequest.getContentType();
		String url =  httpServletRequest.getRequestURI().replace( httpServletRequest.getContextPath(),"");
		//log.info("请求URL:" + url + System.getProperty("line.separator") );

		if (contenttype.length()>15 && contenttype.toLowerCase().contains("multipart/form-data")) {
			this.log.info("====================multipart/form-data 数据类型不解密！");
		}
		else if ("GET".equals(httpServletRequest.getMethod()) && httpServletRequest.getParameter("reqParam")!=null) {
			request=decode(request,response);
		}
		else if ("POST".equals(httpServletRequest.getMethod())  && containsUrl(  url)  ){
			request=decode(request,response);
		}

		chain.doFilter((ServletRequest)request, response);
	}

	protected  boolean containsUrl(String url){
		if(url.contains("."))return  false;
		return  true;
	}


	public ServletRequest decode(ServletRequest request, ServletResponse response) {
		HttpServletRequest httpServletRequest = (HttpServletRequest)request;
		Map<String, String[]> params = new HashMap();
		params.putAll(((ServletRequest)request).getParameterMap());
		String url =  httpServletRequest.getRequestURI().replace( httpServletRequest.getContextPath(),"");

		StringBuffer loginf = new StringBuffer();
		loginf.append(  System.getProperty("line.separator"));
		loginf.append("请求URL :" + url + System.getProperty("line.separator"));
		loginf.append("请求URL参数:" + httpServletRequest.getQueryString() + System.getProperty("line.separator"));
		loginf.append("请求方式:" + httpServletRequest.getMethod() + System.getProperty("line.separator"));
		loginf.append("请求数据CharacterEncoding:" + ((ServletRequest)request).getCharacterEncoding() + System.getProperty("line.separator"));
		this.log.info(loginf.toString());


		if ("GET".equals(httpServletRequest.getMethod())) {
		}

		if ("POST".equals(httpServletRequest.getMethod()) ) {
			ParameterRequestWrapper hostrequest = new ParameterRequestWrapper(httpServletRequest, (Map)null);
			StringBuffer json = new StringBuffer();
			String line = null;

			try {
				BufferedReader reader = hostrequest.getReader();

				while((line = reader.readLine()) != null) {
					json.append(line);
				}
			} catch (Exception var17) {
				;
			}

			this.log.info("====================Reader.json:" + json);
			if (json != null && json.length() > 0) {
				String[] paralist = json.toString().split("&");
				String[] var12 = paralist;
				int var13 = paralist.length;

				for(int var14 = 0; var14 < var13; ++var14) {
					String parastr = var12[var14];
					String[] keyvalue = parastr.split("=");
					if (keyvalue.length == 2) {
						params.put(keyvalue[0], new String[]{keyvalue[1]});
					}
				}
			}
		}

		this.log.info("====================ParamDecodeFilter.doFilter start  params:" + JSON.toJSONString(params));
		if (params.containsKey("reqParam")) {
			this.log.info("====================开始解密 ");
			String[] reqParam = (String[])params.get("reqParam");
			if (reqParam.length > 0) {
				String param = reqParam[0];
				new HashMap();
				String newParamStr = DesUtils.dec2(param, getSecretkey());
				Map<String, Object> newParam = (Map)JSON.parseObject(newParamStr, InParam.class);
				Iterator var23 = newParam.entrySet().iterator();

				while(var23.hasNext()) {
					Map.Entry<String, Object> entry = (Map.Entry)var23.next();
					params.put(entry.getKey(), new String[]{entry.getValue().toString()});
				}
			}
		}

		if (params.containsKey("reqParam")) {
			params.remove("reqParam");
		}

		this.log.info("====================ParamDecodeFilter.doFilter  end  params:" + JSON.toJSONString(params));
		return new ParameterRequestWrapper(httpServletRequest, params);
	}


	public void destroy() {
	}

	public void init(FilterConfig arg0) throws ServletException {
	}
}